import { defineStore } from 'pinia'
import { getBuyGoods } from '@/service/module/user/user'
interface GoodsType {
  goods: any
  page: number
  pageSize: number
  hasMore: boolean
  loading: boolean
  searchParams: any
}
export const useUserBuyStore = defineStore('userBuyStore', {
  state: (): GoodsType => ({
    goods: [],
    page: 1,
    pageSize: 20,
    hasMore: true,
    loading: false,
    searchParams: {},
  }),
  // actions: {
  //   // 获取发布商品列表
  //   async getBuyGoodsList() {
  //     const res = await getBuyGoods()
  //     this.goods = res.data.records
  //   },
  // },
    actions: {
      // 初始化或刷新
      async initGoodsList(params = {}) {
        this.page = 1
        this.hasMore = true
        this.goods = []
        await this.loadMoreGoods({ pageSize: this.pageSize, ...params })
      },
      // 加载更多
      async loadMoreGoods(params = {}) {
        if (this.loading || !this.hasMore) return
        this.loading = true
        const res = await getBuyGoods({ pageNo: this.page, pageSize: this.pageSize, ...params })
        const records = res.data.records || []
        if (this.page === 1) {
          this.goods = records
        } else {
          this.goods = [...this.goods, ...records]
        }
        this.hasMore = records.length === this.pageSize
        this.page += 1
        this.loading = false
      },
    },
})
